package com.study.jyg.rxjava2study.activity;

import android.os.Handler;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.widget.Button;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.adapter.GridLayoutManagerAdapter;
import com.study.jyg.rxjava2study.base.BasicActivity;
import com.study.jyg.rxjava2study.utils.DensityUtil;
import com.study.jyg.rxjava2study.widget.UniversalItemDecoration;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 *
 */
public class GridLayoutManagerActivity extends BasicActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.btnTest1)
    Button btnTest1;
    @BindView(R.id.btnTest2)
    Button btnTest2;
    @BindView(R.id.btnTest3)
    Button btnTest3;
    @BindView(R.id.btnTest4)
    Button btnTest4;
    @BindView(R.id.btnTest5)
    Button btnTest5;
    @BindView(R.id.btnTest6)
    Button btnTest6;

    private GridLayoutManagerAdapter adapter;
    private List<TypeBean> list;
    private RecyclerView.LayoutManager manager;


    @Override
    protected int getLayout() {
        return R.layout.activity_grid_layout_manager;
    }

    @Override
    protected void initView() {
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(v -> finish());
        toolbar.setTitle("Grid导航到指定位置");
        initAdapter();
        btnTest1.setOnClickListener(v -> {
            if (manager instanceof GridLayoutManager){
                recyclerView.smoothScrollToPosition(getposition(0));
//                ((GridLayoutManager) manager).scrollToPositionWithOffset(getposition(0), 0);
//                ((GridLayoutManager) manager).setStackFromEnd(true);
            }else {
                ((LinearLayoutManager) manager).scrollToPositionWithOffset(getposition(0), 0);
                ((LinearLayoutManager) manager).setStackFromEnd(true);
            }
        });
        btnTest2.setOnClickListener(v -> {
            if (manager instanceof GridLayoutManager){
                recyclerView.smoothScrollToPosition(getposition(30));
//                ((GridLayoutManager) manager).scrollToPositionWithOffset(getposition(30), 0);
//                ((GridLayoutManager) manager).setStackFromEnd(true);
            }else {
                ((LinearLayoutManager) manager).scrollToPositionWithOffset(getposition(30), 0);
                ((LinearLayoutManager) manager).setStackFromEnd(true);
            }
        });
        btnTest3.setOnClickListener(v -> {
            if (manager instanceof GridLayoutManager){
                recyclerView.smoothScrollToPosition(getposition(60));
//                ((GridLayoutManager) manager).scrollToPositionWithOffset(getposition(60), 0);
//                ((GridLayoutManager) manager).setStackFromEnd(true);
            }else {
                ((LinearLayoutManager) manager).scrollToPositionWithOffset(getposition(60), 0);
                ((LinearLayoutManager) manager).setStackFromEnd(true);
            }
        });
        btnTest4.setOnClickListener(v -> {
            if (manager instanceof GridLayoutManager){
                recyclerView.smoothScrollToPosition(getposition(90));
//                ((GridLayoutManager) manager).scrollToPositionWithOffset(getposition(90), 0);
//                ((GridLayoutManager) manager).setStackFromEnd(true);
            }else {
                ((LinearLayoutManager) manager).scrollToPositionWithOffset(getposition(90), 0);
                ((LinearLayoutManager) manager).setStackFromEnd(true);
            }
        });
        btnTest5.setOnClickListener(v -> {
            if (manager instanceof GridLayoutManager){
                recyclerView.smoothScrollToPosition(getposition(120));
//                ((GridLayoutManager) manager).scrollToPositionWithOffset(getposition(120), 0);
//                ((GridLayoutManager) manager).setStackFromEnd(true);
            }else {
                ((LinearLayoutManager) manager).scrollToPositionWithOffset(getposition(120), 0);
                ((LinearLayoutManager) manager).setStackFromEnd(true);
            }
        });
        btnTest6.setOnClickListener(v -> {
            if (manager instanceof GridLayoutManager){
                recyclerView.smoothScrollToPosition(getposition(150));
//                ((GridLayoutManager) manager).scrollToPositionWithOffset(getposition(150), 0);
//                ((GridLayoutManager) manager).setStackFromEnd(true);
            }else {
                ((LinearLayoutManager) manager).scrollToPositionWithOffset(getposition(150), 0);
                ((LinearLayoutManager) manager).setStackFromEnd(true);
            }
        });
        new Handler().postDelayed(()->{
           btnTest6.performClick();
        }, 2000);
//        btnOther.setOnClickListener(v -> {
//            int tag = (int) v.getTag();
//            if (tag == MANAGER_LINEAR) {
//                manager = new GridLayoutManager(mContext, 3);
//                btnOther.setTag(MANAGER_GRID);
//            } else {
//                manager = new LinearLayoutManager(mContext);
//                btnOther.setTag(MANAGER_LINEAR);
//            }
//            recyclerView.setLayoutManager(manager);
//            recyclerView.setAdapter(adapter);
//            if (manager instanceof GridLayoutManager){
//                ((GridLayoutManager) manager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//                    @Override
//                    public int getSpanSize(int i) {
//                        int type = recyclerView.getAdapter().getItemViewType(i);
//                        if (type == TypeBean.TITLE || type == TypeBean.DECORATION){
//                            return 3;
//                        }
//                        return 1;
//                    }
//                });
//            }
//        });
    }


    private int getposition(int position){
        return position/3+ position;
    }

    private void initAdapter() {
        list = new ArrayList<>();
        int j = 1;
        for (int i = 0; i < 200; i++) {
            if (i%30 == 0){
                list.add(new TypeBean(TypeBean.TITLE, j++));
            }else if (i%3 == 0){
                list.add(new TypeBean(TypeBean.DECORATION, 0));
            }
            list.add(new TypeBean( TypeBean.GOODS, i));
        }
        manager = new GridLayoutManager(mContext, 3);
        adapter = new GridLayoutManagerAdapter(list);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
//        btnOther.setTag(MANAGER_GRID);
        if (manager instanceof GridLayoutManager){
            ((GridLayoutManager) manager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int i) {
                    int type = recyclerView.getAdapter().getItemViewType(i);
                    if (type == TypeBean.TITLE || type == TypeBean.DECORATION){
                        return 3;
                    }
                    return 1;
                }
            });
        }
        recyclerView.addItemDecoration(new UniversalItemDecoration() {
            @Override
            public Decoration getItemOffsets(int position) {
                ColorDecoration decoration = new ColorDecoration(R.color.colorGrayCC);
                decoration.left = DensityUtil.dip2px(1);
                decoration.right = DensityUtil.dip2px(1);
                return decoration;
            }
        });
    }

    public static class TypeBean implements MultiItemEntity{
        //标题框
        public static final int TITLE = 0;
        //横向分割线
        public static final int DECORATION = 1;
        //商品展示
        public static final int GOODS = 2;
        //item类型
        private int fieldType;

        private Integer text;

        public TypeBean(int fieldType, int text) {
            this.fieldType = fieldType;
            this.text = text;
        }

        @Override
        public int getItemType() {
            return fieldType;
        }

        public Integer getText() {
            return text;
        }

        public void setText(Integer text) {
            this.text = text;
        }
    }

}